import {getXinWen,searchXinWenByTitle,getGongGao,plussXinWenPower} from '@/api/web/news.js'
import {defineStore} from "pinia";
import news = API.news;



const useNewsStore = defineStore(
    'news',
    {
        state: () => ({
            showXinWenList:[] as news[],
            showZhengCe:[] as news[],
            showGongGaoList:[] as news[],
            new:{} as API.news
        }),
        getters:{
            getXinWenList(state){
                getXinWen()
                return state.showXinWenList
            },
            // getGongGaoList(state){
            //     getGongGao()
            //     return state.showGongGaoList
            // }
        },
        actions: {


            //增加新闻的次数
            async plusXinWenPower(newsId:number) {
                return new Promise((resolve, reject) => {
                    console.log("=============增加新闻power")
                    plussXinWenPower(newsId).then((res: any) => {
                        console.log(res)
                        resolve()
                    }).catch((error: any) => {
                        reject(error)
                        console.log(error)
                    })
                })
            },
            //获取新闻
            async getXinWen() {
                return new Promise((resolve, reject) => {
                    console.log("=============getXinWen()")
                    getXinWen().then((res: any) => {
                        console.log(res)
                        this.showXinWenList = res.rows
                        resolve()
                    }).catch((error: any) => {
                        reject(error)
                        console.log(error)
                    })
                })
            },
            //获取公告
            async getGongGao() {
                return new Promise((resolve, reject) => {
                    console.log("=============getGongGao()")
                    getGongGao().then((res: any) => {
                        console.log(res)
                        this.showGongGaoList = res.rows
                        resolve()
                    }).catch((error: any) => {
                        reject(error)
                        console.log(error)
                    })
                })
            },

            async searchXinWenByTitle(title:string){
                return new Promise((resolve, reject) => {
                    console.log("=============searchXinWenByTitle()")
                    searchXinWenByTitle(title).then((res: any) => {
                        console.log(res)
                        this.showXinWenList = res.rows
                        resolve()
                    }).catch((error: any) => {
                        reject(error)
                        console.log(error)
                    })
                })
            }


        },

    })



export default useNewsStore
